package com.gem.service.impl;

import com.gem.domain.Company;
import com.gem.domain.CompanyContact;
import com.gem.mapper.CompanyContactMapper;
import com.gem.mapper.CompanyMapper;
import com.gem.service.CompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-24 14:23
 * @Modified By:
 */
@Service
@SuppressWarnings("all")
@Transactional(rollbackFor=Exception.class)
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper mapper;
    @Autowired
    private CompanyContactMapper contactMapper;

    @Override
    public PageInfo<Company> findAll(int currentPage, int pageSize) {
        PageInfo<Company> pageInfo = null;
        try {
            PageHelper.startPage(currentPage,pageSize);
            pageInfo =new PageInfo<>(mapper.findAll());
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<Company> findAllCompany() {
        try {
            return mapper.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Company findById(Integer coId) {
        return mapper.findById(coId);
    }

    @Override
    public PageInfo<Company> search(Company company, int currentPage, int pageSize) {
        if (company.getCoName() != null) {
            if (company.getCoName().length()==0){
                company.setCoName(null);
            }else {
                company.setCoName(company.getCoName().replaceAll(" ",""));
                company.setCoName("%"+company.getCoName()+"%");
            }
        }
        if (company.getSname()!=null) {
            if (company.getSname().length()==0){
                company.setSname(null);
            }else {
                company.setSname(company.getSname().replaceAll(" ",""));
                company.setSname("%"+company.getSname()+"%");
            }
        }
        if (company.getCname()!=null) {
            if (company.getCname().length()==0){
                company.setCname(null);
            }else {
                company.setCname(company.getCname().replaceAll(" ",""));
                company.setCname("%"+company.getCname()+"%");
            }
        }
        PageInfo<Company> pageInfo = null;
        try {
            PageHelper.startPage(currentPage,pageSize);
            pageInfo =new PageInfo<>(mapper.search(company));
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public Integer save(Company company, CompanyContact companyContact) {
        try {
            company.setCoDate(new Date());
            mapper.save(company);
            companyContact.setCoContactCompanyId(company.getCoId());
            contactMapper.save(companyContact);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return 1;
    }

    @Override
    public Integer update(Company company) {
        return mapper.update(company);
    }

    @Override
    public Integer deleteById(Integer coId) {
        return mapper.deleteById(coId);
    }

    @Override
    @Transactional
    public Integer deleteAll(int[] doIds) {
        Integer result = mapper.deleteAll(doIds);
        return result;
    }

    @Override
    public Company findByName(String stringCellValue) {
        return mapper.findByName(stringCellValue);
    }

    @Override
    public Integer saveExcel(Company company) {
        return mapper.save(company);
    }

    @Override
    public PageInfo<Company> findBycoName(int currentPage, int pageSize,String coName) {
        PageInfo<Company> pageInfo = null;
        try {
            PageHelper.startPage(currentPage,pageSize);
            pageInfo =new PageInfo<Company>(mapper.findBycoName(coName));
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageInfo;
    }
}
